from rest_framework import serializers

from money.models import Money


class MoneyYearAddSerializer(serializers.ModelSerializer):
    """用户个人中心投资收益增加年份"""

    class Meta:
        model = Money
        fields = ('year', 'year_c_money',)

    def create(self, validated_data):
        """
        创建年份
        """
        users_id = self.context['request'].user  # 获取用户对象
        # users_id = validated_data.get('users_id')
        year = validated_data['year']
        year_c_money = validated_data['year_c_money']

        # user = super().create(validated_data)
        user = Money.objects.create(year=year, year_c_money=year_c_money, users_id=users_id)

        return user


class YearListLoadSerializer(serializers.ModelSerializer):
    """用户投资收益序列化器"""

    class Meta:
        model = Money
        fields = (
        'id', 'year', 'year_c_money', 'year_z_add', 'year_z_out', 'dn_year_add', 'dn_year_money', 'year_m_money',
        'dn_year_profit', 'all_money', 'all_profit',)


class YearDeleteDeleteSerializer(serializers.ModelSerializer):
    """删除年份序列化器"""

    class Meta:
        model = Money
        fields = ('id',)


class AddMoneySerializer(serializers.ModelSerializer):
    """提交添加资金的序列化器"""

    class Meta:
        model = Money
        fields = ('year_z_add',)

    def update(self, instance, validated_data):
        year_z_add = round(float(validated_data['year_z_add']), 4)
        year_z_add_old = round(float(instance.year_z_add), 4)
        year_z_out = round(float(instance.year_z_out), 4)
        year_c_money = round(float(instance.year_c_money), 4)

        instance.year_z_add = round(year_z_add + year_z_add_old, 4)
        instance.dn_year_add = round(float(instance.year_z_add) - year_z_out, 4)
        instance.dn_year_money = round(year_c_money + float(instance.dn_year_add), 4)
        instance.dn_year_profit = round(float(instance.year_m_money) - float(instance.dn_year_add) - float(instance.year_c_money), 4)
        instance.save()
        return instance


class OutMoneySerializer(serializers.ModelSerializer):
    """提交取出资金的序列化器"""

    class Meta:
        model = Money
        fields = ('year_z_out',)

    def update(self, instance, validated_data):
        year_z_out = round(float(validated_data['year_z_out']), 4)
        year_z_out_old = round(float(instance.year_z_out), 4)
        year_z_add = round(float(instance.year_z_add), 4)
        year_c_money = float(instance.year_c_money)

        instance.year_z_out = round(year_z_out + year_z_out_old, 4)
        instance.dn_year_add = round(year_z_add - float(instance.year_z_out), 4)
        instance.dn_year_money = round(year_c_money + float(instance.dn_year_add), 4)
        instance.dn_year_profit = round(float(instance.year_m_money) - float(instance.dn_year_add) - float(instance.year_c_money), 4)

        instance.save()
        return instance


class YearMMoneySerializer(serializers.ModelSerializer):
    """提交年末资金的序列化器"""

    class Meta:
        model = Money
        fields = ('year_m_money',)

    def update(self, instance, validated_data):
        year_m_money = round(float(validated_data['year_m_money']), 4)
        instance.year_m_money = year_m_money
        instance.dn_year_profit = round(float(instance.year_m_money) - float(instance.dn_year_add) - float(instance.year_c_money), 4)

        instance.save()
        return instance